package com.dsbj.icardservice.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsbj.common.msg.ResponseMessage;
import com.dsbj.icardservice.entity.Dept;
import com.dsbj.icardservice.entity.Employee;
import com.dsbj.icardservice.entity.EmployeeWelfare;
import com.dsbj.icardservice.mapper.EmployeeMapper;
import com.dsbj.icardservice.mapper.EmployeeWelfareMapper;
import com.dsbj.icardservice.utils.strprocess.ObjectParse;
import com.dsbj.icardservice.utils.strprocess.StrUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cjw 2019-09-24
 */
@Service
@Transactional
public class EmployeeService extends ServiceImpl<EmployeeMapper, Employee> {
	
	@Resource
	private EmployeeMapper employeeMapper;
	
	@Resource
	private EmployeeWelfareMapper employeeWelfareMapper;
	
	
	/**
	 * 根据employeeno查询employee信息。
	 * @param
	 * @return
	 * @author cjw 2019-09-23
	 */
	public Employee getEmployeeByNo(String companyno, String employeeno){
		
		//QueryWrapper<Employee> queryWrapper=new QueryWrapper<>();
		//queryWrapper.eq("EMPLOYEENO",employeeno);
		//Employee employee=employeeMapper.selectOne(queryWrapper);
		
		Employee employee1=employeeMapper.findByCompanynoAndEmployeeno(companyno,employeeno);
		return employee1;
	}
	
	/**
	 * 根据employeeno查询employee信息。
	 * @param
	 * @return
	 * @author cjw 2019-09-23
	 */
	public Employee getEmployeeByNo(String employeeno){
		Employee employee1=employeeMapper.findByEmployeeno(employeeno);
		return employee1;
	}
	
	/**
	 * 根据phoneNum获取对应的employee信息。
	 * @deprecated 待删
	 * @param
	 * @return
	 * @author cjw 2019-10-18
	 */
	@Deprecated
	public ResponseMessage getEmployeeByPhoneNum_0(String phoneNum){
		//获取employeeWelfare信息
		EmployeeWelfare employeeWelfare=employeeWelfareMapper.selectOne(
				new QueryWrapper<EmployeeWelfare>().eq("CONTACTWAY",phoneNum).eq("DELETESTATUS",1)
		);
		//若employeeWelfare为null
		if(employeeWelfare==null){
			return ResponseMessage.error("未找到该手机号码对应的员工信息!");
		}
		
		//根据employeeWelfare.employeeno获取employee信息
		Employee employee=getEmployeeByNo(employeeWelfare.getEmployeeno());
		return ResponseMessage.ok(employee);
	}
	
	/**
	 * 根据手机号码，获取"当前有效的"员工信息。
	 * @param
	 * @return
	 * @author cjw 2019-10-18
	 */
	public ResponseMessage getEmployeeByPhoneNum(String phoneNum){
		//根据手机号码，获取"当前有效的"员工信息
		Employee employee=employeeMapper.findAliveByContactway(phoneNum);
		
		//若employee为null
		if(employee==null){
			return ResponseMessage.error("未找到该手机号码对应的员工信息!");
		}
		return ResponseMessage.ok(employee);
	}
	
	/**
	 * 根据employeenameDim查询employee信息。
	 * @deprecated 待删
	 * @param
	 * @return
	 * @author cjw 2019-09-24
	 */
	public List<Employee> getEmployeeByEmployeenameDim_0(String companyno, String employeenameDim){
		
		/* //连表查询
		Employee employee_input=new Employee();
		employee_input.setEmployeenameDim(employeenameDim);
		employee_input.setDeleteStatus(1);
		
		List<Employee> list_employee=null;
		list_employee=(List<Employee>) employeeMapper.findByMap(ObjectParse.convertObject_toMap(employee_input));
		return list_employee;
		*/
		
		//未联表查询
		QueryWrapper<Employee> queryWrapper=new QueryWrapper<>();
		queryWrapper.select("ID","EMPLOYEENO","EMPLOYEENAME").eq("COMPANYNO",companyno)
				.and(i->i.like("EMPLOYEENO",employeenameDim).or().like("EMPLOYEENAME",employeenameDim))
				.isNull("DISMISSIONDATE").eq("DELETESTATUS",1);
		
		List<Employee> employees=employeeMapper.selectList(queryWrapper);
		return employees;
	}
	
	/**
	 * 根据employeenameDim模糊查询employee信息。
	 * @param
	 * @return
	 * @author cjw 2019-09-24
	 */
	public List<Employee> getEmplByEmployeenameDim(String companyno, String employeenameDim){
		
		QueryWrapper<Employee> queryWrapper=new QueryWrapper<>();
		queryWrapper.select("ID","EMPLOYEENO","EMPLOYEENAME").eq("COMPANYNO",companyno)
				.like("EMPLOYEENAME",employeenameDim)
				.isNull("DISMISSIONDATE").eq("DELETESTATUS",1).apply("ROWNUM<100");
		
		List<Employee> employees=employeeMapper.selectList(queryWrapper);
		return employees;
	}
	
	/**
	 * 根据employeenoDim模糊查询employee信息。
	 * @param
	 * @return
	 * @author cjw 2019-09-24
	 */
	public List<Employee> getEmplByEmployeenoDim(String companyno, String employeenoDim){
		
		QueryWrapper<Employee> queryWrapper=new QueryWrapper<>();
		queryWrapper.select("ID","EMPLOYEENO","EMPLOYEENAME").eq("COMPANYNO",companyno)
				.like("EMPLOYEENO",employeenoDim)
				.isNull("DISMISSIONDATE").eq("DELETESTATUS",1).apply("ROWNUM<100");
		
		List<Employee> employees=employeeMapper.selectList(queryWrapper);
		return employees;
	}
	
	
	
	
	
	
	
}
